package com.jingbit.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.RequiresApi;

public class CustomBaseDialog extends Dialog {

    private Window window;
    private DisplayMetrics metrics;

    public CustomBaseDialog(Context context, int themeRes) {
        super(context, themeRes);
        metrics = new DisplayMetrics();
    }

    public void showDialog(int gravity, int anim) {
        if (isShowing()) {
            dismiss();
        }
        showDialog(gravity, anim, 0.9f, 0);
    }

    public void showDialog(int gravity, int anim, float w, float h) {
        if (isShowing()) {
            dismiss();
        }
        show();
        windowDeploy(gravity, anim, w, h);
    }


    /**
     * @param gravity
     * @param anim
     * @param w       全屏为1,自适应为0
     * @param h       全屏为1,自适应为0
     */
    //设置窗口显示
    public void windowDeploy(int gravity, int anim, float w, float h) {
        window = getWindow(); //得到对话框
        window.setWindowAnimations(anim); //设置窗口弹出动画
        window.setBackgroundDrawableResource(R.color.transparent); //设置对话框背景为透明
        WindowManager.LayoutParams wl = window.getAttributes();
        window.getWindowManager().getDefaultDisplay().getRealMetrics(metrics);
        //根据x，y坐标设置窗口需要显示的位置
//        wl.x = x; //x小于0左移，大于0右移
//        wl.y = y; //y小于0上移，大于0下移
//            wl.alpha = 0.6f; //设置透明度
        wl.width = w == 0 ? WindowManager.LayoutParams.WRAP_CONTENT : (int) (metrics.widthPixels * w);
        wl.height = h == 0 ? WindowManager.LayoutParams.WRAP_CONTENT : (int) (metrics.heightPixels * h);
        Log.d("jingbit", "windowDeploy: " + wl.width + "---" + wl.height);
        wl.gravity = gravity; //设置重力
        window.setAttributes(wl);
    }

}